<template>
	<div class="add"><el-button type="primary" @click="add">添加</el-button></div>
	<el-dialog
		    v-model="dialogVisible_add"
		    title="Tips"
		    width="500"
		  >
		    <el-input type="text" v-model="url" placeholder="请输入添加的图片url"></el-input>
			<el-select
			      v-model="goodsValue"
			      placeholder="请选择商品"
			      size="large"
			      style="width: 240px"
			    >
			      <el-option
			        v-for="item in options"
			        :key="item.id"
			        :label="item.name"
			        :value="item.id"
			      />
			    </el-select>
		    <template #footer>
		      <div class="dialog-footer">
		        <el-button @click="dialogVisible_add = false">Cancel</el-button>
		        <el-button type="primary" @click="addSubmit">
		          Confirm
		        </el-button>
		      </div>
		    </template>
	</el-dialog>
	<el-dialog
		    v-model="dialogVisible_update"
		    title="Tips"
		    width="500"
		  >
		    <el-input type="text" v-model="url" placeholder="请输入添加的图片url"></el-input>
		<el-select
		      v-model="goodsValue"
		      placeholder="请选择商品"
		      size="large"
		      style="width: 240px"
		    >
		      <el-option
		        v-for="item in options"
		        :key="item.id"
		        :label="item.name"
		        :value="item.id"
		      />
		    </el-select>
		    <template #footer>
		      <div class="dialog-footer">
		        <el-button @click="dialogVisible_update = false">Cancel</el-button>
		        <el-button type="primary" @click="updateSubmit">
		          Confirm
		        </el-button>
		      </div>
		    </template>
	</el-dialog>
	<el-dialog
	  v-model="dialogVisible_del"
	  title="Tips"
	  width="500"
	>
	  确认要删除 {{delName}} 吗？
	  <template #footer>
	    <div class="dialog-footer">
	      <el-button @click="dialogVisible_del = false">Cancel</el-button>
	      <el-button type="primary" @click="delSubmit">
	        Confirm
	      </el-button>
	    </div>
	  </template>
	</el-dialog>
	<el-table :data="tableData" style="width: 1200px;">
	  <el-table-column prop="name" label="商品名"></el-table-column>
	  <el-table-column label="图片">
		  <template #default="scope">
			  <img :src="scope.row.url" style="width:100px;height:100px;"></img>
		  </template>
	  </el-table-column>

	  <el-table-column prop="createTime" label="创建时间" />
	  <el-table-column prop="updateTime" label="更新时间" />
			<el-table-column>
				<template #header>操作</template>
				<template #default="scope">
				  <el-button type="warning" @click="update(scope.row.goodsId,scope.row.url)">编辑</el-button>
				  <el-button type="danger" @click="del(scope.row.id,scope.row.name)">删除</el-button>
				</template>
			</el-table-column>
	</el-table>
</template>

<script setup>
	import {ref} from 'vue'
	const tableData = ref([])
	import {http} from "../components/http.js"
	const dialogVisible_add = ref(false)
	function query(){
		http({url:"/querySwiper",method:"get"}).then(res=>{
			tableData.value = res.data.data
		})
	}
	query()
	const url = ref("")
	const goodsValue = ref(undefined)
	const options = ref([])
	function add(){
		dialogVisible_add.value = true
		http({url:"/queryGoods",method:"get"}).then(res=>{
			options.value = res.data.data
		})
	}
	const dialogVisible_del = ref(false)
	const delName = ref("")
	function addSubmit(){
		http({url:"/addSwiper",method:"post",data:{url:url.value}}).then(res=>{
			
		})
	}
	const delId = ref(undefined)
	function del(id,name){
		dialogVisible_del.value = true
		delName.value = name
		delId.value = id
	}
	function delSubmit(){
		http({url:"/delSwiper?id="+delId.value,method:"get"}).then(res=>{
			dialogVisible_del.value = false
			query()
		})
	}
	const dialogVisible_update = ref(false)
	function update(goodsId,u){
		dialogVisible_update.value = true
		http({url:"/queryGoods",method:"get"}).then(res=>{
			options.value = res.data.data
		})
		goodsValue.value = goodsId
		url.value = u
	}
	function updateSubmit(){
		http({url:"/updateSwiper",method:"post",data:{}}).then(res=>{
			
		})
	}
</script>

<style>
</style>